package com.boomsecret.juc.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/22.
 */
public class ExecutorServiceDemo02 {
    private static ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    public static void main(String[] args) {
        Future<String> future = threadPool.submit(() -> {
            try {
                System.out.println("runnable start.");
                TimeUnit.SECONDS.sleep(2);
                System.out.println("runnable end.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "success");

        try {
            String result = future.get(1, TimeUnit.SECONDS);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        threadPool.shutdown();
        System.out.println("主线程结束.");
    }
}
